package com.flyqiu.flow.model.entity.hi;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.flyqiu.common.tools.LocalDateTimeTools;
import com.flyqiu.flow.common.std.InstActivityComplete;
import com.flyqiu.flow.model.entity.ru.ProcRuActivity;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.Duration;
import java.time.LocalDateTime;

@EqualsAndHashCode(callSuper = true)
@Data
@TableName(ProcHiActivity.TABLE_NAME)
public class ProcHiActivity extends ProcRuActivity implements InstActivityComplete {
    public static final String TABLE_NAME = "FLY_FLOW_HI_ACT";
    @TableField("END_TIME_")
    private LocalDateTime endTime;
    @TableField("DURATION_")
    private Long duration;



    public static ProcHiActivity create(ProcRuActivity ruActivity, LocalDateTime endTime) {
        return create(ruActivity, ruActivity.getAssignee(), endTime);
    }

    public static ProcHiActivity create(ProcRuActivity ruActivity, String assignee, LocalDateTime endTime) {
        ProcHiActivity result = create(ruActivity);
        result.setAssignee(assignee);
        if (endTime == null) {
            return result;
        }
        result.endTime = endTime;
        result.duration = Duration.between(result.getCreateTime(), result.getEndTime()).toMillis();
        return result;
    }

    public static ProcHiActivity create(ProcRuActivity ruActivity) {
        ProcHiActivity result = new ProcHiActivity();
        result.setId(ruActivity.getId());
        result.setProcInstId(ruActivity.getProcInstId());
        result.setActType(ruActivity.getActType());
        result.setDefKey(ruActivity.getDefKey());
        result.setDefName(ruActivity.getDefName());
        result.setAssignee(ruActivity.getAssignee());
        result.setAssigneeKey(ruActivity.getAssigneeKey());
        result.setCreateTime(ruActivity.getCreateTime());
        result.setEndTime(LocalDateTime.now());
        result.setActInstId(result.getActInstId());
        result.setDuration(0L);
        return result;
    }

    @Override
    public void completeAssignee(String assignee, String assigneeKey) {
        setAssignee(assignee);
        if (assigneeKey == null) {
            assigneeKey = "";
        }
        setAssigneeKey(assigneeKey);
    }

    @Override
    public void completeEndTime(LocalDateTime endTime) {
        setEndTime(endTime);
        duration = LocalDateTimeTools.timeDiffMillis(getCreateTime(), endTime);
    }
}
